package crawl

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/chromedp/chromedp"

	"github.com/gocolly/colly/v2"
)

func GetPage2(baseUrl string, page_call func(resp *colly.Response)) error {
	// Instantiate default collector
	c := colly.NewCollector(
		// MaxDepth is 2, so only the links on the scraped page
		// and links on those pages are visited
		colly.MaxDepth(2),
		colly.Async(),
	)
	c.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"

	// Limit the maximum parallelism to 2
	// This is necessary if the goroutines are dynamically
	// created to control the limit of simultaneous requests.
	//
	// Parallelism can be controlled also by spawning fixed
	// number of go routines.
	c.Limit(&colly.LimitRule{DomainGlob: "*", Parallelism: 2, RandomDelay: 5 * time.Second})
	// var title string
	// var content string
	var err error
	// c.OnHTML("title", func(e *colly.HTMLElement) {
	// 	title = e.Text
	// 	fmt.Print(title)
	// })

	// c.OnHTML("article", func(e *colly.HTMLElement) {
	// 	content = e.Text
	// })

	c.OnError(func(r *colly.Response, e error) {
		err = e
	})

	c.OnRequest(func(r *colly.Request) {
	})

	c.OnResponse(page_call)

	// Start scraping on https://en.wikipedia.org
	e := c.Visit(baseUrl)
	if e != nil {
		fmt.Println(e)
	}
	// Wait until threads are finished
	c.Wait()
	if err != nil {
		return err
	}
	return nil
}

func GetPage(baseUrl string) (string, error) {
	// 设置超时时间
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	// 配置浏览器选项
	opts := append(chromedp.DefaultExecAllocatorOptions[:],
		chromedp.Flag("headless", true),                  // 无头模式
		chromedp.Flag("disable-gpu", true),               // 禁用 GPU 加速
		chromedp.Flag("ignore-certificate-errors", true), // 忽略证书错误
		chromedp.WindowSize(1200, 800),                   // 设置窗口大小
	)

	allocCtx, allocCancel := chromedp.NewExecAllocator(ctx, opts...)
	defer allocCancel()

	// 创建浏览器上下文
	taskCtx, taskCancel := chromedp.NewContext(allocCtx)
	defer taskCancel()

	// 导航到目标页面并等待加载完成
	err := chromedp.Run(taskCtx,
		chromedp.Navigate(baseUrl),
		chromedp.Sleep(3*time.Second), // 等待页面加载完成
	)
	if err != nil {
		log.Fatal(err)
	}

	// 获取完整的 HTML 内容
	var htmlContent string
	err = chromedp.Run(taskCtx,
		chromedp.OuterHTML("html", &htmlContent), // 获取整个 <html> 标签的内容
	)
	if err != nil {
		log.Fatal(err)
	}

	return htmlContent, nil
}
